$(function () {
  var nickName = sessionStorage.getItem("nickname");
  if (!nickName) {
    $(".off").addClass("active").siblings().removeClass("active");
  } else {
    $(".on").addClass("active").siblings().removeClass("active");
    $(".nickname").text(nickName);
  }

  axios
    .get("http://localhost:9000/carousel/list")
    .then(function (response) {
      if (response.data.code === 1) {
        var imagesHtml = response.data.list
          .map(function (item) {
            return `<div><img src="http://localhost:9000/${item.name}" /></div>`;
          })
          .join("");
        $("#carousel div").html(imagesHtml);
        layui.carousel.render({
          elem: "#carousel",
          width: "800px",
          height: "400px",
          arrow: "hover",
          anim: "fade",
        });
      }
    })
    .catch(function (error) {
      console.warn("error:", JSON.stringify(error));
    });

  $(".self").on("click", function () {
    window.location.href = "./self.html";
  });

  $(".logout").on("click", function () {
    var token = sessionStorage.getItem("token");
    var id = sessionStorage.getItem("id");
    axios
      .get("http://localhost:9000/users/logout", {
        headers: {
          Authorization: token,
        },
        params: {
          id,
        }
      })
      .then(function (response) {
        if (response.data.code === 1) {
          sessionStorage.clear();
          window.location.href = "./index.html";
        }
      })
      .catch(function (error) {
        console.error("error:", error);
      });
  });
});
